package com.situ.web.service.impl;

import com.situ.web.dao.IBanjiDao;
import com.situ.web.dao.impl.BanjiDaoImpl;
import com.situ.web.pojo.Banji;
import com.situ.web.service.IBanjiService;
import com.situ.web.util.PageInfo;

import java.util.List;

public class BanjiServiceImpl implements IBanjiService {
    private IBanjiDao banjiDao = new BanjiDaoImpl();

    @Override
    public List<Banji> selectAll() {
        return banjiDao.selectAll();
    }

    @Override
    public void deleteById(Integer id) {
        banjiDao.deleteById(id);
    }

    /**
     * Service业务，为了完成一个复杂的操作，需要好几步流程，
     * 可能需要调用多次dao操作
     */
    @Override
    public void add(Banji banji) {
        banjiDao.add(banji);
    }

    @Override
    public Banji selectById(Integer id) {
        return banjiDao.selectById(id);
    }

    @Override
    public void update(Banji banji) {
        banjiDao.update(banji);
    }

    @Override
    public PageInfo<Banji> selectByPage(Integer pageNo, Integer pageSize) {
        //第一条sql：查询当前页的数据
        int offset = (pageNo - 1) * pageSize;
        List<Banji> list = banjiDao.selectByPage(offset, pageSize);
        //第二条sql：查询总的数量
        int totalCount = banjiDao.selectTotalCount();
        int totalPage = (int) Math.ceil((double)totalCount / pageSize);

        PageInfo<Banji> pageInfo = new PageInfo(list, totalPage, pageNo, pageSize);
        return pageInfo;
    }
}
